package com.assistant.service.impl;

import com.assistant.common.DateUtil;
import com.assistant.mapper.AddressPosterMapper;
import com.assistant.mapper.PostPackageMapper;
import com.assistant.pojo.AddressPoster;
import com.assistant.pojo.PostPackage;
import com.assistant.service.PostPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PostPackageServiceImpl implements PostPackageService {
    @Autowired
    private PostPackageMapper postPackageMapper;
    @Autowired
    private AddressPosterMapper addressPosterMapper;

    @Override
    public int insertPostPackage(PostPackage postPackage) {
        postPackage.setPost_time(DateUtil.dataFormatUtil(new Date()));
        postPackage.setStatus(4L);
        return postPackageMapper.insertSelective(postPackage);
    }

    @Override
    public int updatePostPackage(PostPackage postPackage) {
        postPackage.setUpdate_time(DateUtil.dataFormatUtil(new Date()));
        return postPackageMapper.updateByPrimaryKeySelective(postPackage);
    }

    @Override
    public int cancelPostPackage(Long id) {
        PostPackage postPackage = new PostPackage();
        postPackage.setStatus(3L);
        postPackage.setPackage_id(id);
        return postPackageMapper.updateByPrimaryKeySelective(postPackage);
    }


    public List<AddressPoster> showAllPostPackage() {
        Example example = new Example(AddressPoster.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", 0);
        return addressPosterMapper.selectByExample(example);
    }

    public List<AddressPoster> selectPostPackageByx(Map<String, String> map) {
        Example example = new Example(AddressPoster.class);
        Example.Criteria criteria = example.createCriteria();
        if (map.get("package_id") != null) {
            criteria.andEqualTo("package_id", map.get("package_id"));
        }
        if (map.get("poster_id") != null) {
            criteria.andEqualTo("poster_id", map.get("poster_id"));
        }
        System.out.println(map.get("status") );
        if (map.get("status") != null) {
            criteria.andEqualTo("status", map.get("status"));
        }
        if(map.get("size") != null){
            criteria.andEqualTo("size",map.get("size"));
        }
        if(map.get("money")!=null){
            criteria.andEqualTo("money",map.get("money"));
        }
        return addressPosterMapper.selectByExample(example);
    }

    public int updateGetterMsg(Long id, Long address_id) {
        PostPackage postPackage = new PostPackage();
        postPackage.setPackage_id(id);
        postPackage.setAddress_id(address_id);
        return postPackageMapper.updateByPrimaryKeySelective(postPackage);
    }

    @Override
    public int deletePackage(Integer id) {
        return postPackageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateStatus(Integer id, Integer status) {
        PostPackage postPackage = new PostPackage();
        postPackage.setPackage_id(Long.valueOf(id));
        postPackage.setStatus(Long.valueOf(status));
        return postPackageMapper.updateByPrimaryKeySelective(postPackage);
    }
}
